
#include "ObjectManager.h"

/*
 *
 */
IObjectManager* ObjectManager::_objectManager;

/* Constructor
 */
ObjectManager::ObjectManager() :
	_playerObjectList(NULL),
	_autoObjectList(NULL),
	_stationaryObjectList(NULL),
	_wallList(NULL),
	_playerObjectCount(0),
	_autonomousObjectCount(0),
	_stationaryObjectCount(0),
	_wallCount(0)
{ 
}

/* Destructor
 */
ObjectManager::~ObjectManager()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::~ObjectManager() ");
	}

	//Delete any objects remaining in _movingObjectList

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::~ObjectManager() ");
	}
}

/* Initialize()
 *
 */
void ObjectManager::Initialize()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::Initialize() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_playerObjectCount = " + Ogre::StringConverter::toString(_playerObjectCount));
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_autonomousObjectCount = " + Ogre::StringConverter::toString(_autonomousObjectCount));
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_stationaryObjectCount = " + Ogre::StringConverter::toString(_stationaryObjectCount));	
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::Initialize() ");
	}
}

/* UpdateStarted(const Ogre::FrameEvent &gameTime)
 *
 */
void ObjectManager::UpdateStarted(const Ogre::FrameEvent &gameTime)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() >= 4 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
		//Output only every 120 frames
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::UpdateStarted(const Ogre::FrameEvent &gameTime) ");
	}
	
	if(_playerObjectList)
	{
		for(std::vector<IMovingObject*>::iterator it = _playerObjectList->begin(); it != _playerObjectList->end(); it++)
		{
			(*it)->UpdateStarted(gameTime);
		}
	}

	if(_autoObjectList)
	{
		for(std::vector<IMovingObject*>::iterator it = _autoObjectList->begin(); it != _autoObjectList->end(); it++)
		{
			(*it)->UpdateStarted(gameTime);
		}
	}

	if(GameServices::GameEngine()->GetLoggingLevel() >= 4 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
		//Output only every 120 frames
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::UpdateStarted(const Ogre::FrameEvent &gameTime) ");
	}
}

/* UpdateEnded(const Ogre::FrameEvent &gameTime)
 *
 */
void ObjectManager::UpdateEnded(const Ogre::FrameEvent &gameTime)
{ 
}

/* Cleanup()
 * TODO
 */
void ObjectManager::Cleanup()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::Cleanup() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::Cleanup() ");
	}
}

/* AddPlayerObject
 * Creates a new PlayerObject and pushes it on
 * the _playerObjectList. For this project only
 * one PlayerObject should be created in a state.
 * AddPlayerObject(double xpos, double ypos, double zpos) enforces this limit.
 */
void ObjectManager::AddPlayerObject(double xpos, double ypos, double zpos)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::AddPlayerObject(double xpos, double ypos, double zpos) ");
	}

	//if _playerObjectList is not initialized
	if(!_playerObjectList)	
	{
		//Create a new vector for _playerObjectList to point to
		_playerObjectList = new std::vector<IMovingObject*>();
	}

	//Push one new player object on the vector
	_playerObjectList->push_back(new PlayerObject());

	//Create the scene node and entity for the player object
	Ogre::SceneNode *aSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode(
		"player" + Ogre::StringConverter::toString(_playerObjectCount)));
	Ogre::Entity *anEntity(GameServices::OgreManager()->GetSceneManager()->createEntity(
		"player" + Ogre::StringConverter::toString(_playerObjectCount) , "sphere.mesh"));
	aSceneNode->attachObject(anEntity);
	aSceneNode->setScale(1,1,1);
	aSceneNode->setPosition(xpos, ypos, zpos);
	_playerObjectList->at(_playerObjectCount)->SetPostion(Ogre::Vector3(xpos, ypos, zpos));
	_playerObjectList->at(_playerObjectCount)->SetName("player" + Ogre::StringConverter::toString(_playerObjectCount));
	anEntity->setMaterialName("Examples/RustySteel");

	_playerObjectCount++;

	if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### Player " + Ogre::StringConverter::toString(_playerObjectCount) + " created #####");
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_playerObjectCount = " + Ogre::StringConverter::toString(_playerObjectCount));
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::AddPlayerObject(double xpos, double ypos, double zpos) ");
	}
	return;
}

/* RemovePlayerObject()
 *
 */
void ObjectManager::RemovePlayerObject()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::RemovePlayerObject() ");
	}

	//If the list of players doesn't exists print error and return
	if(!_playerObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _playerObjectList does not exists !!! Error !!!");
		return;
	}
	//If the list doesn't have a player print error and return
	else if(_playerObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no players to remove !!! Error !!!");
		return;
	}
	//If there are PlayerObjects in the list delete them and clear the list
	else 
	{
		for(int i = 0; i < _playerObjectCount; i++)
		{
			_playerObjectList->at(i)->Cleanup();	//Clean up the PlayerObject

			delete _playerObjectList->at(i);		//Delete the PlayerObject held in the list

			//Destroy the entity
			GameServices::OgreManager()->GetSceneManager()->destroyEntity("player" + Ogre::StringConverter::toString(i));

			//Destroy the scene node
			GameServices::OgreManager()->GetSceneManager()->destroySceneNode("player" + Ogre::StringConverter::toString(i));


			if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
			{
				Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### Player "+ Ogre::StringConverter::toString(i) +" Removed #####");
				Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_playerObjectCount = " + Ogre::StringConverter::toString(_playerObjectCount));
			}	
		}

		_playerObjectList->clear();	//Clear the playerList of all slots

		_playerObjectCount = 0;
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::RemovePlayerObject() ");
	}
}

/* GetPlayerObject(int id)
 *
 */
IMovingObject* ObjectManager::GetPlayerObject(int id)
{
	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetPlayerObject(int id) ");
	}

	//Since this is madatory one player game error if the id is not 0
	if(id < 0 || id > 1)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! Invalid player ID !!! Error !!!");
	}
	//If the list of players doesn't exists print error
	else if(!_playerObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _playerObjectList does not exists !!! Error !!!");
	}
	//If the list doesn't have a player print error
	else if(_playerObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no players in the list !!! Error !!!");
	}
	else
	{
		if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
		{
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetPlayerObject(int id) ");
		} 

		//return a pointer to the object at vector index[id]
		return _playerObjectList->at(id);
	}

	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetPlayerObject(int id) ");
	} 
	return NULL;
}

/* GetPlayerList()
 *
 */
std::vector<IMovingObject*>* ObjectManager::GetPlayerList()
{
	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetPlayerList() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetPlayerList() ");
	}

	return _playerObjectList;
}

/* AddAutonomousObject(double xpos, double ypos, double zpos)
 *
 */
void ObjectManager::AddAutonomousObject(double xpos, double ypos, double zpos)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::AddAutonomousObject(double xpos, double ypos, double zpos) ");
	}

	//if _autoObjectList is not initialized
	if(!_autoObjectList)	
	{
		//Create a new vector for _autoObjectList to point to
		_autoObjectList = new std::vector<IMovingObject*>();
	}

	//Push one new auto object on the vector
	_autoObjectList->push_back(new AutoObject());
	_autoObjectList->at(_autonomousObjectCount)->Initialize();

	//Create the scene node and entity for the auto object
	Ogre::SceneNode *aSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("auto" + 
		Ogre::StringConverter::toString(_autonomousObjectCount)));
	Ogre::Entity *anEntity(GameServices::OgreManager()->GetSceneManager()->createEntity(
		"auto" + Ogre::StringConverter::toString(_autonomousObjectCount) , "sphere.mesh"));
	aSceneNode->attachObject(anEntity);
	aSceneNode->setScale(1,1,1);
	aSceneNode->setPosition(xpos, ypos, zpos);
	_autoObjectList->at(_autonomousObjectCount)->SetPostion(Ogre::Vector3(xpos, ypos, zpos));
	_autoObjectList->at(_autonomousObjectCount)->SetName("auto" + Ogre::StringConverter::toString(_autonomousObjectCount));
	anEntity->setMaterialName("Examples/GreenSkin");

	_autonomousObjectCount++;		//Increment the autonomous object count

	if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### Autonomous Object Created #####");
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_autonomousObjectCount = " + Ogre::StringConverter::toString(_autonomousObjectCount));
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::AddAutonomousObject(double xpos, double ypos, double zpos) ");
	}
	return;
}

/* RemoveAutonomousObject()
 *
 */
void ObjectManager::RemoveAutonomousObject()
{ 
	
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::RemoveAutonomousObject() ");
	}

	//If the list of auto objects doesn't exists print error and return
	if(!_autoObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _autoObjectList does not exists !!! Error !!!");
		return;
	}
	//If the list doesn't have a player print error and return
	else if(_autoObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no autonomous objects to remove !!! Error !!!");
		return;
	}
	//If there is a PlayerObject in the list delete it and clear the list
	else
	{
		for(std::vector<IMovingObject*>::iterator it = _autoObjectList->begin(); it != _autoObjectList->end(); it++)
		{
			(*it)->Cleanup();
		}

		for(int i = 0; i < _autonomousObjectCount; i++)
		{
			//Destroy the entity
			GameServices::OgreManager()->GetSceneManager()->destroyEntity("auto" + Ogre::StringConverter::toString(i));

			//Destroy the scene node
			GameServices::OgreManager()->GetSceneManager()->destroySceneNode("auto" + Ogre::StringConverter::toString(i));
			delete _autoObjectList->at(i);
		}

		_autoObjectList->clear();

		_autonomousObjectCount = 0;

		if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
		{
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### All Autonomous Objects Removed #####");
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_autonomousObjectCount = " + Ogre::StringConverter::toString(_playerObjectCount));
		}	
	}
	
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::RemoveAutonomousObject() ");
	}
}

/* GetAutonomousObject(int id)
 *
 */
IMovingObject* ObjectManager::GetAutonomousObject(int id)
{ 
	
	if(GameServices::GameEngine()->GetLoggingLevel() >= 4 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetAutonomousObject(int id) ");
	}

	//Check to make sure a valid id was passed in
	if(id < 0 || id >= _autonomousObjectCount)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! Invalid auto ID !!! Error !!!");
	}
	//If the list of auto object doesn't exists print error
	else if(!_autoObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _autoObjectList does not exists !!! Error !!!");
	}
	//If the list doesn't have a auto object print error
	else if(_autoObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no auto objects in the list !!! Error !!!");
	}
	else
	{
		if(GameServices::GameEngine()->GetLoggingLevel() == 5)	//debug
		{
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetAutonomousObject(int id) ");
		} 

		//return a pointer to the object at vector index[id]
		return _autoObjectList->at(id);
	}

	if(GameServices::GameEngine()->GetLoggingLevel() >= 4 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetAutonomousObject(int id) ");
	}
	return NULL;
}

/* GetAutonomousList()
 *
 */
std::vector<IMovingObject*>* ObjectManager::GetAutonomousList()
{
	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetAutonomousList() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() == 5 && 
		GameServices::OgreManager()->GetRoot()->getNextFrameNumber() % 120 == 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetAutonomousList() ");
	}

	return _autoObjectList;
}

/* AddStationaryObject()
 *
 */
void ObjectManager::AddStationaryObject(double xpos, double ypos, double zpos)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::AddStationaryObject() ");
	}

	//if _stationaryObjectList is not initialized
	if(!_stationaryObjectList)	
	{
		//Create a new vector for _stationaryObjectList to point to
		_stationaryObjectList = new std::vector<IObject*>();
	}

	//Push one new stationary object on the vector
	_stationaryObjectList->push_back(new StationaryObject());
	_stationaryObjectList->at(_stationaryObjectCount)->Initialize();

	//Create the scene node and entity for the stationary object
	Ogre::SceneNode *aSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("stationary" + 
		Ogre::StringConverter::toString(_stationaryObjectCount)));
	Ogre::Entity *anEntity(GameServices::OgreManager()->GetSceneManager()->createEntity(
		"stationary" + Ogre::StringConverter::toString(_stationaryObjectCount) , "cube.mesh"));
	aSceneNode->attachObject(anEntity);
	aSceneNode->setScale(5,5,1);
	aSceneNode->setPosition(xpos, ypos, zpos);
	_stationaryObjectList->at(_stationaryObjectCount)->SetPostion(Ogre::Vector3(xpos, ypos, zpos));
	_stationaryObjectList->at(_stationaryObjectCount)->SetName("stationary" + Ogre::StringConverter::toString(_stationaryObjectCount));
	_stationaryObjectList->at(_stationaryObjectCount)->SetBRadius(500.0);

	_stationaryObjectCount++;		//Increment the stationary object count

	if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### Stationary Object Created #####");
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_stationaryObjectCount = " + Ogre::StringConverter::toString(_stationaryObjectCount));
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::AddStationaryObject(double xpos, double ypos, double zpos) ");
	}
	return;


	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::AddStationaryObject() ");
	}
}

/* RemoveStationaryObject(int id)
 *
 */
void ObjectManager::RemoveStationaryObject(int id)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::RemoveStationaryObject(int id) ");
	}

	//If the list of objects doesn't exists print error and return
	if(!_stationaryObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _stationaryObjectList does not exists !!! Error !!!");
		return;
	}
	//If the list doesn't have any objects
	else if(_stationaryObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no stationary objects to remove !!! Error !!!");
		return;
	}
	//If there is a list clear it
	else if(id < 0)
	{
		for(std::vector<IObject*>::iterator it = _stationaryObjectList->begin(); it != _stationaryObjectList->end(); it++)
		{
			(*it)->Cleanup();
		}

		for(int i = 0; i < _stationaryObjectCount; i++)
		{
			//Destroy the entity
			GameServices::OgreManager()->GetSceneManager()->destroyEntity("stationary" + Ogre::StringConverter::toString(i));

			//Destroy the scene node
			GameServices::OgreManager()->GetSceneManager()->destroySceneNode("stationary" + Ogre::StringConverter::toString(i));
			delete _stationaryObjectList->at(i);
		}

		_stationaryObjectList->clear();

		_stationaryObjectCount = 0;

		if(GameServices::GameEngine()->GetLoggingLevel() != 0)	//debug
		{
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### All Stationary Objects Removed #####");
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_stationaryObjectCount = " + Ogre::StringConverter::toString(_playerObjectCount));
		}	
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::RemoveStationaryObject(int id) ");
	}
}

/* GetStationaryObject(int id)
 *
 */
IObject* ObjectManager::GetStationaryObject(int id)
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() >= 4)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetStationaryObject(int id) ");
	}

	//Check to make sure a valid id was passed in
	if(id < 0 || id >= _stationaryObjectCount)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! Invalid stationary ID !!! Error !!!");
	}
	//If the list of auto object doesn't exists print error
	else if(!_stationaryObjectList)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _stationaryObjectList does not exists !!! Error !!!");
	}
	//If the list doesn't have a auto object print error
	else if(_stationaryObjectList->size() <= 0)
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no stationary objects in the list !!! Error !!!");
	}
	else
	{
		if(GameServices::GameEngine()->GetLoggingLevel() == 5)	//debug
		{
			Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetStationaryObject(int id) ");
		} 

		//return a pointer to the object at vector index[id]
		return _stationaryObjectList->at(id);
	}

	if(GameServices::GameEngine()->GetLoggingLevel() >= 4)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetStationaryObject(int id) ");
	}
	return NULL;
}

/* GetStationaryList()
 *
 */
std::vector<IObject*>* ObjectManager::GetStationaryList()
{
	if(GameServices::GameEngine()->GetLoggingLevel() == 5)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetStationaryList() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() == 5)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetStationaryList() ");
	}
	return _stationaryObjectList;
}

/* AddWall(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D)
 *
 */
void ObjectManager::AddWall(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D)
{
        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::AddWall(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D)) ");
        }

        //if _stationaryObjectList is not initialized
        if(!_wallList)  
        {
            //Create a new vector for _stationaryObjectList to point to
            _wallList = new std::vector<IWall*>();
        }

        //Push one new wall object on the vector
        _wallList->push_back(new Wall3D(A,B,C,D));
        _wallList->at(_wallCount)->Initialize();

        //Create the scene node and entity for the wall object
        Ogre::SceneNode *aSceneNode(GameServices::OgreManager()->GetSceneManager()->getRootSceneNode()->createChildSceneNode("wall" + 
                Ogre::StringConverter::toString(_wallCount)));
        Ogre::Entity *anEntity(GameServices::OgreManager()->GetSceneManager()->createEntity(
                "wall" + Ogre::StringConverter::toString(_wallCount) , "cube.mesh"));
        aSceneNode->attachObject(anEntity);
		aSceneNode->setScale((B-A).length()/100,1,1);											//Scale the wall by the length of the vector / length of the actual mesh
		aSceneNode->rotate(Ogre::Vector3::UNIT_Z, (B-A).angleBetween(Ogre::Vector3::UNIT_X));	//Rotate the wall by the appropriate
        aSceneNode->setPosition(((A+B)/2.0) + C/2 + D/2);										//Set the position according to the center of the vector
        _wallList->at(_wallCount)->SetName("wall" + Ogre::StringConverter::toString(_wallCount));

        _wallCount++;           //Increment the stationary object count

        if(GameServices::GameEngine()->GetLoggingLevel() != 0)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### Wall Object Created #####");
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_wallObjectCount = " + Ogre::StringConverter::toString(_wallCount));
        }

        if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::AddWall(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D) ");
        }
        return;
}

/* RemoveWall(int id)
 *
 */
void ObjectManager::RemoveWall(int id)
{
        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::RemoveWall(int id) ");
        }

        //If the list of objects doesn't exists print error and return
        if(!_wallList)
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _wallList does not exists !!! Error !!!");
                return;
        }
        //If the list doesn't have any objects
        else if(_wallList->size() <= 0)
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no wall objects to remove !!! Error !!!");
                return;
        }
        //If the id passed in is negative clear all the objects from the list
        else if(id < 0)
        {
                for(std::vector<IWall*>::iterator it = _wallList->begin(); it != _wallList->end(); it++)
                {
                        (*it)->Cleanup();
                }

                for(int i = 0; i < _wallCount; i++)
                {
                        //Destroy the entity
                        GameServices::OgreManager()->GetSceneManager()->destroyEntity("wall" + Ogre::StringConverter::toString(i));

                        //Destroy the scene node
                        GameServices::OgreManager()->GetSceneManager()->destroySceneNode("wall" + Ogre::StringConverter::toString(i));
                        delete _wallList->at(i);
                }

                _wallList->clear();

                _wallCount = 0;

                if(GameServices::GameEngine()->GetLoggingLevel() != 0)  //debug
                {
                        Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("##### All Wall Objects Removed #####");
                        Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("_wallCount = " + Ogre::StringConverter::toString(_playerObjectCount));
                }       
        }

        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::RemoveWall(int id) ");
        }
}

/* GetWall(int id)
 *
 */
IWall* ObjectManager::GetWall(int id)
{
        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetWall(int id) ");
        }

        //Check to make sure a valid id was passed in
        if(id < 0 || id >= _wallCount)
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! Invalid wall ID !!! Error !!!");
        }
        //If the list of auto object doesn't exists print error
        else if(!_wallList)
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! _wallList does not exists !!! Error !!!");
        }
        //If the list doesn't have a auto object print error
        else if(_wallList->size() <= 0)
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("!!! Error !!! There are no wall objects in the list !!! Error !!!");
        }
        else
        {
                if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
                {
                        Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetWallObject(int id) ");
                } 

                //return a pointer to the object at vector index[id]
                return _wallList->at(id);
        }

        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetWall(int id) ");
        }
}

/* GetWallList()
 *
 */
std::vector<IWall*>* ObjectManager::GetWallList()
{
        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetWallList() ");
        }

        if(GameServices::GameEngine()->GetLoggingLevel() == 5)  //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetWallList() ");
        }
        return _wallList;
}


/* GetPlayerObjectCount()
 *
 */
int ObjectManager::GetPlayerObjectCount()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetPlayerObjectCount() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetPlayerObjectCount() ");
	}
	return _playerObjectCount;
}
 
/* GetAutonomousObjectCount()
 *
 */
int ObjectManager::GetAutonomousObjectCount()
{
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetAutonomousObjectCount() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetAutonomousObjectCount() ");
	}
	return _autonomousObjectCount;
}

/* GetStationaryObjectCount()
 *
 */
int ObjectManager::GetStationaryObjectCount()
{ 
	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetStationaryObjectCount() ");
	}

	if(GameServices::GameEngine()->GetLoggingLevel() > 1)	//debug
	{
		Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetStationaryObjectCount() ");
	}

	return _stationaryObjectCount;
}

/* GetWallCount()
 *
 */
int ObjectManager::GetWallCount()
{ 
        if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::GetWallCount() ");
        }

        if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::GetWallCount() ");
        }

        return _wallCount;
}

/* LineIntersection(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D, double &dist, Ogre::Vector3 &point)
 * From Buckland LineIntersection2D
 */
bool ObjectManager::LineIntersection(Ogre::Vector3 A, Ogre::Vector3 B, Ogre::Vector3 C, Ogre::Vector3 D, double &dist, Ogre::Vector3 &point)
{
        if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== ENTER ObjectManager::LineIntersection() ");
        }

		double rTop, rBot, sTop, sBot, r, s;

		rTop = (A.y - C.y) * (D.x - C.x) - (A.x - C.x) * (D.y - C.y);
		rBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);

		sTop = (A.y - C.y) * (B.x - A.x) - (A.x - C.x) * (B.y - A.y);
		sBot = (B.x - A.x) * (D.y - C.y) - (B.y - A.y) * (D.x - C.x);

		//If rBot or sBot equals 0 then the lines are parallel and there is no IP
		if(rBot == 0 || sBot == 0)
		{
			return false;
		}

		r = rTop/rBot;
		s = sTop/sBot;

		if(r > 0 && r < 1 && s > 0 && s < 1)
		{
			dist = (A - B).length() * r;
			point = A + r * (B-A);
			return true;
		}
		else
		{
			dist = 0;
		}

        if(GameServices::GameEngine()->GetLoggingLevel() > 1)   //debug
        {
                Ogre::LogManager::getSingleton().getLog("Dlog.txt")->logMessage("========== EXIT ObjectManager::LineIntersection() ");
        }
		return false;
}